<!DOCTYPE html>
<html lang="en"  xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title>menu</title>
    <link rel="stylesheet" href="../../../../lib/layui-v2.6.3/css/layui.css" media="all">
    <link rel="stylesheet" href="../../../../css/public.css" media="all">
    <style>
        .layui-btn:not(.layui-btn-lg ):not(.layui-btn-sm):not(.layui-btn-xs) {
            height: 34px;
            line-height: 34px;
            padding: 0 8px;
        }
    </style>
</head>
<body>
<div class="layuimini-container">
    <div class="layuimini-main">
        <!--<%&#45;&#45;<blockquote class="layui-elem-quote">
            Layui的树形表格treeTable，支持异步加载(懒加载)、复选框联动、折叠状态记忆。<br>
            <a href="https://gitee.com/whvse/treetable-lay" target="_blank" class="layui-btn layui-btn-danger">treetable-lay</a>
        </blockquote>&#45;&#45;%>-->
        <div>
            <div class="layui-btn-group">
                <button class="layui-btn" id="btn-expand">全部展开</button>
                <button class="layui-btn layui-btn-normal" id="btn-fold">全部折叠</button>
            </div>
            <table id="munu-table" class="layui-table" lay-filter="munu-table"></table>
        </div>
    </div>
</div>
<!-- 操作列 -->
<script type="text/html" id="auth-state">
    <button class="layui-btn  layui-btn-xs" lay-event="add" > 添加 </button>
    <a class="layui-btn layui-btn-primary layui-btn-xs" lay-event="edit">修改</a>
    <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del">删除</a>
</script>

<script src="../../../../lib/layui-v2.6.3/layui.js" charset="utf-8"></script>
<script src="../../../../js/lay-config.js?v=1.0.4" charset="utf-8"></script>
<script>
    layui.use(['table', 'treetable'], function () {
        var $ = layui.jquery;
        var table = layui.table;
        var treetable = layui.treetable;

        // 渲染表格
        layer.load(2);
        treetable.render({
            height: 'full-140',//差值，表格容器距离浏览器顶部和底部的距离“和”
            treeColIndex: 1,//树形图标显示在第几列
            treeSpid: 0,//最上级的父级id
            treeIdName: 'id',//id字段的名称
            treePidName: 'parentId',
            elem: '#munu-table',
            url: '../../../sys/menu/queryAll',

            cols: [[
                {type: 'numbers'},
                {field: 'title', minWidth: 200, title: '菜单名称'},
                {field: 'authCode', title: '权限标识'},
                {field: 'url', title: '菜单url'},
                {field: 'orderNum', width: 80, align: 'center', title: '排序号'},
                {
                    field: 'type', width: 80, align: 'center', templet: function (d) {
                        if (d.type == 2) {
                            return '<span class="layui-badge layui-bg-gray">按钮</span>';
                        }
                        if (d.parentId == 0) {
                            return '<span class="layui-badge layui-bg-blue">目录</span>';
                        } else {
                            return '<span class="layui-badge-rim">菜单</span>';
                        }
                    }, title: '类型'
                },
                {templet: '#auth-state', width: 200, align: 'center', title: '操作'}
            ]],
            done: function () {
                layer.closeAll('loading');
            }
        });

        $('#btn-expand').click(function () {
            treetable.expandAll('#munu-table');
        });

        $('#btn-fold').click(function () {
            treetable.foldAll('#munu-table');
        });


        //监听工具条
        table.on('tool(munu-table)', function (obj) {
            var data = obj.data;
            var layEvent = obj.event;
            if (layEvent === 'del') {
               layer.confirm("真的要删除么？",function (index){
                   $.ajax({
                       url:"../../../sys/menu/deleteById",
                       data:"id="+data.id,
                       dataType:"json",
                       success:function(result){
                           layer.msg(result.msg);
                           if (result.code==0){
                              // layer.treetable.reload('#munu-table')
                               window.location.reload();
                           }
                       }
                   });
                   layer.close(index);
               })
            } else if (layEvent === 'edit') {
                var index=layer.open({
                    title:'编辑菜单',
                    type:2,
                    shade:0.2,
                    mxamin:true,
                    area:['100%','100%'],
                    content:"menu_edit.html",
                    success: function(layero, index) {
                        var body = layer.getChildFrame('body', index);
                        //得到iframe页的窗口对象，执行iframe页的方法：iframeWin.method();
                        var iframeWin = window[layero.find('iframe')[0]['name']];
                        // console.log(body.html()) //得到iframe页的body内容

                        body.find("input[name='id']").val(data.id);
                        body.find("input[name='title']").val(data.title);
                        body.find("input[name='authCode']").val(data.authCode);
                        body.find("input[name='url']").val(data.url);
                        body.find("input[name='orderNum']").val(data.orderNum);
                        body.find("input[name='type'][value='"+data.type+"']").prop("checked",true);



                        //调用子页面的方法，传递父页面的参数给子页面
                        //iframeWin.init(data);
                        //重新渲染
                        iframeWin.layui.form.render();
                    }
                });

            }
            else if (obj.event === 'add') {
                var index = layer.open({
                    title: '添加菜单',
                    type: 2,
                    shade: 0.2,
                    maxmin: true,
                    shadeClose: true,
                    area: ['100%', '100%'],
                    content: 'menu_add.html',
                    success: function (layero, index) {
                        var body = layer.getChildFrame('body', index);
                        var iframeWin = window[layero.find('iframe')[0]['name']];
                        body.find("input[name='parentId']").val(data.id);
                        iframeWin.layui.form.render();
                    }
                });
                $(window).on("resize", function () {
                    layer.full(index);
                });
            }
        });
    });
</script>
</body>
</html>